﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using SocialCee.EDM;
using System.Data.Entity;

namespace SocialCee.DAL
{
    public class Posts
    {
        /// <summary>
        /// Gets posts
        /// </summary>
        /// <param name="includeComments">Optional param</param>
        /// <param name="top">Optional param</param>
        /// <returns></returns>
        public static List<SocialNetworkPost> GetPosts(bool includeComments=false, int top = 20)
        {
            using (SocialCeeDB_Entities context = new SocialCeeDB_Entities())
            {
                IQueryable<SocialNetworkPost> posts = context.SocialNetworkPost.AsQueryable();
                if (includeComments)
                    posts = posts.Include("SocialNetworkPostComments");
                List<SocialNetworkPost> postList = posts.OrderByDescending(snp => snp.Date).Take(top).ToList();
                
                //trick for sort subqueries
                foreach (SocialNetworkPost post in postList)
                    post.SocialNetworkPostComments = post.SocialNetworkPostComments.OrderByDescending(c => c.Date).ToList();

                return postList;
            }
        }

    }
}
